#!/bin/bash

# 安装 Nginx
color_echo blue "正在安装 Nginx..."
apt-get install nginx -y
systemctl enable nginx
systemctl start nginx

# 安装 vsftpd
color_echo blue "正在安装 vsftpd..."
apt-get install vsftpd -y
systemctl enable vsftpd
systemctl start vsftpd

# 安装 MySQL
color_echo blue "正在安装 MySQL..."
apt-get install mysql-server -y
systemctl enable mysql
systemctl start mysql

# 安装 PHP 最新版本
color_echo blue "正在安装 PHP..."
apt-get install software-properties-common -y
add-apt-repository ppa:ondrej/php -y
apt-get update -y
apt-get install php-fpm php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip -y
systemctl enable php-fpm
systemctl start php-fpm

# 安装 Docker
color_echo blue "正在安装 Docker..."
apt-get install docker.io -y
systemctl enable docker
systemctl start docker

# 创建 /web/html 目录
color_echo blue "正在创建 /web/html 目录..."
mkdir -p /web/html
chown -R www-data:www-data /web/html
chmod -R 755 /web/html

# 配置 Nginx（如果配置文件不存在）
color_echo blue "正在配置 Nginx..."
if [ ! -f /etc/nginx/sites-available/default ]; then
    cat <<EOF > /etc/nginx/sites-available/default
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /web/html;
    index index.php index.html index.htm;

    server_name _;

    location / {
        try_files \$uri \$uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php-fpm.sock;
        fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT \$document_root;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

    error_page 404 /404.html;
    location = /404.html {
        internal;
    }
}
EOF
    systemctl restart nginx
else
    color_echo yellow "Nginx 配置文件已存在，跳过覆盖。"
fi

# 创建 FTP 用户并设置随机密码
FTP_USER="ftpuser"
FTP_PASS=$(openssl rand -base64 12)
color_echo blue "正在创建 FTP 用户..."
if id "$FTP_USER" &>/dev/null; then
    color_echo yellow "FTP 用户已存在，跳过创建。"
else
    useradd -m -d /web/html -s /usr/sbin/nologin $FTP_USER
    echo "$FTP_USER:$FTP_PASS" | chpasswd
    chown -R $FTP_USER:$FTP_USER /web/html
fi

# 配置 vsftpd（如果配置文件不存在）
color_echo blue "正在配置 vsftpd..."
if ! grep -q "local_root=/web/html" /etc/vsftpd.conf; then
    sed -i 's/#write_enable=YES/write_enable=YES/g' /etc/vsftpd.conf
    sed -i 's/#local_umask=022/local_umask=022/g' /etc/vsftpd.conf
    echo "local_root=/web/html" >> /etc/vsftpd.conf
    echo "chroot_local_user=YES" >> /etc/vsftpd.conf
    echo "allow_writeable_chroot=YES" >> /etc/vsftpd.conf
    systemctl restart vsftpd
else
    color_echo yellow "vsftpd 配置文件已包含必要配置，跳过修改。"
fi

# 生成随机数据库用户名、密码和数据库名
DB_USER="user_$(openssl rand -hex 3)"
DB_PASS="pass_$(openssl rand -hex 8)"
DB_NAME="db_$(openssl rand -hex 4)"

# 配置 MySQL
color_echo blue "正在配置 MySQL..."
mysql -e "CREATE DATABASE ${DB_NAME};"
mysql -e "CREATE USER '${DB_USER}'@'localhost' IDENTIFIED BY '${DB_PASS}';"
mysql -e "GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '${DB_USER}'@'localhost';"
mysql -e "FLUSH PRIVILEGES;"

# 调整 PHP 配置文件以允许访问系统其他文件
color_echo blue "正在调整 PHP 配置..."
PHP_VERSION=$(php -v | grep -oP 'PHP \K[0-9]+\.[0-9]+')
PHP_INI_PATH="/etc/php/$PHP_VERSION/fpm/php.ini"
if [ -f "$PHP_INI_PATH" ]; then
    sed -i 's/^open_basedir =.*/open_basedir = none/' "$PHP_INI_PATH"
    systemctl restart php-fpm
    color_echo green "PHP 配置调整完成！"
else
    color_echo red "未找到 PHP 配置文件，请手动检查。"
fi

# 创建 .htaccess 文件并设置权限
color_echo blue "正在创建 .htaccess 文件..."
cat <<EOF > /web/html/.htaccess
# 防止目录列表
Options -Indexes
EOF

# 设置 .htaccess 文件权限为只允许读写和修改，不允许删除
chmod 644 /web/html/.htaccess
chattr +i /web/html/.htaccess

# 打印配置信息
color_echo green "安装和配置完成！以下是详细信息："
echo "================================================"
echo "FTP 用户名：$FTP_USER"
echo "FTP 密码：$FTP_PASS"
echo "数据库名称：${DB_NAME}"
echo "数据库用户：${DB_USER}"
echo "数据库密码：${DB_PASS}"
echo "PHP 运行环境已安装并配置完成。"
echo "Docker 容器环境已安装并配置完成。"
echo "所有服务已设置为开机自启动。"
echo "================================================"

# 完成
color_echo green "安装完毕！"